package tom.vertx.core.demo03;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.rxjava.core.json.pointer.JsonPointer;
import lombok.Data;

import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * {@link io.vertx.rxjava.core.json.pointer.JsonPointer} rx-java
 * {@link io.vertx.core.json.JsonObject} vertx json类
 * @author ZHUFEIFEI
 */
public class JsonDemo {

  public static void main(String[] args) {
    //JsonObject
    String jsonString = "{\"foo\":\"bar\"}";
    JsonObject object = new JsonObject(jsonString);
    System.out.println(object);

    Map<String, Object> map = new HashMap<>();
    map.put("foo", "bar");
    map.put("xyz", 3);
    object = new JsonObject(map);
    object.put("foo1", "bar1").put("num", 123).put("mybool", true);
    System.out.println(object);

    System.out.println(object.getInteger("num"));

    object = new JsonObject().put("name", "tom");
    System.out.println(object.mapTo(A.class));

    //Json
    Object obj = Json.decodeValue(jsonString);
    System.out.println(obj instanceof JsonObject);
    System.out.println(obj instanceof JsonArray);

    //vertx-rx-java JsonPointer json指针采用/jsonTag方式访问操作json元素
    JsonPointer pointer1 = JsonPointer.from("/age");
    System.out.println(pointer1.writeJson(object, "123"));
    // = /name
    JsonPointer pointer = JsonPointer.create().append("name");
    System.out.println(pointer.writeJson(object, "newName"));
    System.out.println(pointer.queryJson(object));
    System.out.println(object);

  }

  @Data
  static class A {
    String name = "A";
  }
}
